package com.company.clientscard.main.widgets.clientform;

import java.util.Date;

import com.company.clientscard.main.widgets.general.TableManager;
import com.company.clientscard.shared.proxy.DocumentProxy;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DocumentTable extends VerticalPanel {

	private TableManager<DocumentProxy> tableManager;

	private CellTable<DocumentProxy> table = new CellTable<DocumentProxy>();

	private String getFormattedDate(Date date) {
		DateTimeFormat fmt = DateTimeFormat.getFormat("dd.MM.yyyy");
		if (date == null) {
			return "";
		} else {
			return fmt.format(date);
		}
	}

	private String getFormattedNumber(Long number) {
		NumberFormat fmt = NumberFormat.getFormat("############.00");
		double n = 0.00;
		if (number == null) {
			n = 0.00;
		} else {
			n = number / 100;
		}

		return fmt.format(n);
	}

	private TextColumn<DocumentProxy> docDateColumn = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return getFormattedDate(object.getDocDate());
		}
	};
	private TextColumn<DocumentProxy> docNumColumn = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return object.getDocNum();
		}
	};

	private TextColumn<DocumentProxy> docTypeColumn = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return object.getDocType();
		}
	};

	private TextColumn<DocumentProxy> docSumColumn = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return getFormattedNumber(object.getSum());
		}
	};

	private TextColumn<DocumentProxy> docPrinyat = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return getFormattedDate(object.getPrinyat());
		}
	};

	private TextColumn<DocumentProxy> docGotov = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return getFormattedDate(object.getGotov());
		}
	};

	private TextColumn<DocumentProxy> docVidan = new TextColumn<DocumentProxy>() {
		@Override
		public String getValue(DocumentProxy object) {
			return getFormattedDate(object.getVidan());
		}
	};

	private void initTable() {
		table.addColumn(docDateColumn, "Date");
		table.addColumn(docNumColumn, "Number");
		table.addColumn(docTypeColumn, "Type");
		table.addColumn(docSumColumn, "SUM");
		table.addColumn(docPrinyat, "Prinyat");
		table.addColumn(docGotov, "Gotov");
		table.addColumn(docVidan, "Vidan");

		table.setWidth("100%", true);
		table.setColumnWidth(docDateColumn, 5.0, Unit.PCT);
		table.setColumnWidth(docDateColumn, 10.0, Unit.PCT);

		// table.setPageSize(25);
		//
		// SimplePager pager = new SimplePager();
		// pager.setPageSize(5);
		// pager.setDisplay(table);

		setWidth("100%");
		setHorizontalAlignment(ALIGN_CENTER);
		add(table);
		// add(pager);

	}

	public DocumentTable(TableManager<DocumentProxy> tableManager) {
		this.tableManager = tableManager;
		initTable();

		tableManager.addDataDisplay(table);
	}

	public TableManager<DocumentProxy> getTableManager() {
		return tableManager;
	}
}
